/**
 * 功能描述
 */

define(function (require, exports, module) {
	var store = require("store");
	var activeUser = store.get("user");
	var Dialog=require("dialog");
	var tools = require("tools");
	var aw = require("ajaxwrapper");
	var MemTransferForm = require("../component/MemTransferForm");
    module.exports = {
    	grid:{
    		showDetail:function(widget,ret,data){
    			MemTransferForm.renderForm(widget,false);
    			var form = widget.get("form"),
    				endForm = widget.get("endForm"),
    				subGrid = widget.get("subGrid");
    			var receiver = data.receiver;
    			widget.toForm();
    			endForm.setData(receiver);    			
    			subGrid.setData(ret);
    			
    			var birthday = tools.getBirthdateByIdNo(receiver.personalInfo.idNumber);
				var age = tools.getAgeByBirthdate(birthday);
				endForm.setValue("age",age);
				endForm.setValue("remark",data.remark)
				if(receiver.personalInfo.contactAddresses[0] && receiver.personalInfo.contactAddresses[0].length>0){
					endForm.setValue("address", receiver.personalInfo.contactAddresses[0].address);
					endForm.setValue("areaAddress", receiver.personalInfo.contactAddresses[0].detailAddress);	
				}
				
    			if (receiver.personalInfo.lunarBirthday) {
                    $("#check").attr("checked", true);
                } else {
                    $("#check").attr("checked", false);
                }
    			
    			form.setValue("memberCertificates",data.travelContract.memberCertificates);
    			form.setValue("contactNo",data.travelContract.contactNo);
    			form.setValue("balance",data.travelContract.balance == null ? 0 : data.travelContract.balance);
    			form.setValue("personalInfo.name",data.assignor.personalInfo.name);
    			form.setValue("personalInfo.sex",data.assignor.personalInfo.sex);
    			form.setValue("personalInfo.idNumber",data.assignor.personalInfo.idNumber);
    			form.setValue("productRule",data.travelContract.productRule.product.name);
    			form.setValue("productType",data.travelContract.productType.name);
    			form.setValue("transferType",data.transferType.value);
    			form.setValue("signUpDate",data.travelContract.signUpDate);
    			form.setValue("status.value",data.travelContract.status.value);
    			form.setValue("balTime",data.travelContract.balTime);
    			form.setValue("unit",data.travelContract.unit);
    			
    			$(".J-form-endForm-save").addClass("hidden");
    			$(".J-form-endForm-cancel").addClass("hidden");
    			endForm.setDisabled(true);
    			form.setDisabled(true);
    		}
    	},
    	form:{
    		searchContact:function(widget,memberCertificates){
    			var form = widget.get("form"),
    				subGrid = widget.get("subGrid");
    			widget.service.queryTravelcontract({
    				"travelContract.servicePoint":activeUser.servicePoint.pkServicePoint,
    				"travelContract.memberCertificates":memberCertificates,
               		fetchProperties:widget.constant.travelContract
    			},function(data){
    				if(data == ""){
    					Dialog.alert({
    						content:"您输入的会员证号不正确，请重新输入！",
    						confirm:function(){
    							form.reset();
    							subGrid.setData("");
    						}
    					});
    				}else{
    					var ret = data[0];
    						productRule = ret.travelContract.productRule.product.name,
    						pkProductType = ret.travelContract.productType.pkProductType,
    						productType = ret.travelContract.productType.name
    						personalInfoName = ret.travelContract.member.personalInfo.name,
    						idNumber = ret.travelContract.member.personalInfo.idNumber,
    						phone = ret.travelContract.member.personalInfo.mobilePhone,
    						pkPersonalInfo = ret.travelContract.member.personalInfo.pkPersonalInfo;
    						balance = ret.travelContract.balance;
    						balTime = ret.travelContract.balTime;
    						
    						
    						
    					form.setData(ret.travelContract);
    					form.setValue("balance", balance == null ? 0 : balance);
    					form.setValue("personalInfo.name", personalInfoName);
    					form.setValue("personalInfo.idNumber", idNumber);
    					form.setValue("personalInfo.phone", phone);
						form.setValue("productRule", productRule);
						form.setValue("productType", productType);
						form.setValue("balTime", balTime);
						if(ret.travelContract.unit != null){
							form.setValue("unit",ret.travelContract.unit.value);
						}
    					form.setValue("personalInfo.pkPersonalInfo", pkPersonalInfo);
    					subGrid.setData(ret.welfareStatisticsViews);
    					
    				}
    			})
    		}
    	},
    	endForm:{
    		searchMember:function(widget,idNumber){
    			var endForm = widget.get("endForm");
    			widget.service.queryMemberData({
    				"personalInfo.idNumber":idNumber, 
					fetchProperties:widget.constant.personalInfo
    			},function(data){
    				endForm.reset();
    				var birthday = tools.getBirthdateByIdNo(idNumber);
					var age = tools.getAgeByBirthdate(birthday);
					endForm.setValue("personalInfo.birthday",birthday);
					endForm.setValue("age",age);
    				if(data == ""){
    					endForm.setValue("personalInfo.idNumber",idNumber),
    					endForm.setValue("isMem",false),
    					endForm.setDisabled(["personalInfo.name","personalInfo.sex","personalInfo.mobilePhone",
    					                     "personalInfo.birthday","personalInfo.birthday","address","areaAddress",
    					                     "personalInfo.zipCode"],false);
    				}else{
    					var ret = data[0];
    					endForm.setData(ret);
    					endForm.setValue("isMem",true);
    					if (ret.personalInfo.lunarBirthday) {
                            $("#check").attr("checked", true);
                        } else {
                            $("#check").attr("checked", false);
                        }
    					endForm.setValue("address", ret.personalInfo.contactAddresses[0].address);
    					endForm.setValue("areaAddress", ret.personalInfo.contactAddresses[0].detailAddress);
    					endForm.setDisabled(["personalInfo.name","personalInfo.sex","personalInfo.mobilePhone",
    					                     "personalInfo.birthday","address","areaAddress",
    					                     "personalInfo.zipCode"],true);
    				}
    			})
    		},
    		saveactionEvent:function(widget){
    			var form = widget.get("form"),
    				formData = form.getData(),
    				endForm = widget.get("endForm"),
    				endFormData = endForm.getData();
    			
				if(form.valid()){
					var saveData = {};
					var isMem = endFormData.isMem;
					var receiver;
					var name = endForm.getValue("personalInfo.name"),
						sex = endForm.getValue("personalInfo.sex"),
						birthday = endForm.getValue("personalInfo.birthday"),
						idNumber = endForm.getValue("personalInfo.idNumber"),
						mobilePhone = endForm.getValue("personalInfo.mobilePhone"),
						zipCode = endForm.getValue("personalInfo.zipCode");
                    if ($('#check').is(':checked')) {
                    	lunarBirthday = true;
                    } else {
                    	lunarBirthday = false;
                    }
					
					if(isMem == "true"){
						receiver = endFormData.pkMember
					}else{
						receiver = {
							organization: endFormData.organization,
							status: 'Normal',
							version:0,
							deleteFlag:'UnDeleted',
							createData:endFormData.operateDate,
							creater:endFormData.operator,
							source:'Offline',
							dataSource:'Organization',
							personalInfo:{
								name: name,
								sex: sex,
								birthday: birthday,
								lunarBirthday: lunarBirthday,
								idType: "IdentityCard",
								idNumber: idNumber,
								zipCode: zipCode,
								version:0,
								mobilePhone:mobilePhone
							}
						};
					}
					saveData = {
						travelContract: formData.pkTravelContract,
						transferType: formData.transferType,
						workCost: formData.workCost,
						remark: endFormData.remark,
						operateDate: endFormData.operateDate,
						operator: endFormData.operator,
						receiver: receiver,
						address: endFormData.address,
						detailAddress: endFormData.areaAddress,
						addressStatus:"DefaultAddress",
						version:0
					};
					widget.service.saveactionData(aw.customParam(saveData),function(data){
						widget.toGrid();
						widget.get("grid").refresh();
					})
				}
    		}
    	}
    };
});